/**
* ...
* @author Default
* @version 0.1
*/

package com.vfd.motion {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent
	import flash.events.Event; 

	public class Thrower extends Sprite {
		private var _clip:Sprite; 
		private var gravity:Number = .5; 
		private var oldX:Number;
		private var oldY:Number;
		private var xSpeed:Number =0;
		private var ySpeed:Number= 1;
		private var bounce:Number = -0.7;
		private var _leftBound:Number = 0;
		private var _topBound:Number = 0;
		private var _rightBound:Number = 0;
		private var _bottomBound:Number = 0;
		public function Thrower(clip:DisplayObject) {
			this._clip = clip as Sprite; 
			this._clip.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
			this.addEventListener(Event.ENTER_FRAME, bounceClip);
		}
		private function bounceClip(evt:Event) {
			this.ySpeed += this.gravity; 
			this._clip.x += this.xSpeed;
			this._clip.y += this.ySpeed;
			if (this._clip.x + this._clip.width > this._rightBound) {
				trace("Clip has exceeded right bound")
				this._clip.x = this._rightBound - this._clip.width;
				this.xSpeed *= this.bounce; 
			}else if (this._clip.x - this._clip.width < this._leftBound) {
				trace("Clip has exceeded left bound")
				this._clip.x = this._leftBound + this._clip.width; 
				this.xSpeed *= this.bounce;  
			}
			
			if (this._clip.y + this._clip.height > this._bottomBound) {
				trace("Clip has exceeded bottom bound")
				this._clip.y = this._bottomBound - this._clip.height; 
				this.ySpeed *= this.bounce; 
			}else if (this._clip.y - this._clip.height < this._topBound) {
			trace("Clip has exceeded top bound")
				this._clip.y = this._topBound + this._clip.height; 
				this.ySpeed *= this.bounce;  
			}
			trace("X speed " + this.xSpeed + " y speed " + this.ySpeed + " clip x " + this._clip.x + " clip y " + this._clip.y)
		}
		private function onMouseDown(evt:MouseEvent) {
			this.oldX = this._clip.x; 
			this.oldY = this._clip.y; 
			this._clip.addEventListener(MouseEvent.MOUSE_UP, throwClip); 
			this._clip.startDrag(); 
			removeEventListener(Event.ENTER_FRAME, bounceClip)			
		}

		private function throwClip(evt:MouseEvent) {
			this._clip.removeEventListener(MouseEvent.MOUSE_UP, throwClip); 
			this._clip.stopDrag(); 
			this.xSpeed = this._clip.x - this.oldX
			this.ySpeed = this._clip.x - this.oldY
			addEventListener(Event.ENTER_FRAME, bounceClip)
		}
		public function setBounds(top:Number, right:Number, bottom:Number, left:Number){
			this._topBound = top; 
			this._rightBound = right; 
			this._bottomBound = bottom; 
			this._leftBound = left; 
			this._clip.y = this._bottomBound; 
		}
	}
	
}
